package com.example.demo.controller;

import com.example.demo.Interface.UserRepo;
import com.example.demo.entity.User;
import com.example.demo.entity.Wendu;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
public class Register {

    @Resource
    UserRepo userRepo;
    @RequestMapping(value = "/register1")
    public String create(){
        return "register";
    }


    @RequestMapping(value = "/register")
    @ResponseBody
    public String register(HttpServletRequest req, HttpServletResponse rsp) {

        String name = req.getParameter("name");
        String password = req.getParameter("password");
       // System.out.print(name);


        //判断是否已存在该用户
        List<User> optionalUser = userRepo.findAll();
        for (User user : optionalUser) {
            String name1 = user.name;
            if(name.equals(name1)){
              return "repeat" ;//用户已存在
            }
        }
        User user = new User();
        user.name = name;
        user.password = password;
        userRepo.save(user);
        //注册成功
        return "resuccess";


    }
}
